﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_News_Default : System.Web.UI.Page
{

    private List<NewsCategory> cates = new List<NewsCategory>();


    private void BuildTree()
    {
        TreeNode root = new TreeNode
        {
            Text = "Danh s\x00e1ch chuy\x00ean mục tin",
            NodeID = "-1",
            Expanded = true,
            Icon = Ext.Net.Icon.House
        };
        this.tpListNewsCategory.Root.Add(root);
        this.tpListNewsCategory.RootVisible = false;
        this.cates = new NewsCategoryRepository().GetAll();
        foreach (NewsCategory nc in this.cates.FindAll(p => p.ParentId == 0).ToList<NewsCategory>())
        {
            root.Nodes.Add(this.createChildrenNode(nc));
        }
        TreeNode all = new TreeNode
        {
            Text = "Tất cả",
            NodeID = "0"
        };
        root.Nodes.Add(all);
    }

    private TreeNode createChildrenNode(NewsCategory nc)
    {
        TreeNode node = new TreeNode
        {
            Text = nc.Name,
            NodeID = nc.ID.ToString().Trim(),
            Expanded = true
        };
        List<NewsCategory> childs = (from p in this.cates.FindAll(p => p.ParentId == nc.ID)
                                     orderby p.Name
                                     select p).ToList<NewsCategory>();
        if (childs.Count != 0)
        {
            foreach (NewsCategory nc1 in childs)
            {
                node.Nodes.Add(this.createChildrenNode(nc1));
            }
        }
        return node;
    }

    public void Delete(object sender, DirectEventArgs e)
    {
        try
        {
            NewsArticleRepository repository = new NewsArticleRepository();
            List<NewsArticle> ads = new List<NewsArticle>();
            CheckboxSelectionModel selectionModel = (CheckboxSelectionModel)this.GridPanel1.SelectionModel.Primary;
            foreach (SelectedRow row in selectionModel.SelectedRows)
            {
                repository.Delete(int.Parse(row.RecordID));
            }
            repository.SubmitChanges();
            this.PagingToolBar1.PageIndex = 1;
            ExtNet.Msg.Hide();
            Util.ShowMessage(Constants.UPDATE_SUCCESS);
        }
        catch
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ExtNet.IsAjaxRequest)
        {
            this.BuildTree();
        }
    }
}